package zunge.tools;

import java.sql.*;

public class DbConn {
	public Connection conn;
	public PreparedStatement pstmt;
	
	public DbConn(String host, String db_name, String user_name, String password){
		try{
			String db_url = "jdbc:mysql://" + host + "/" + db_name +
				"?useUnicode=true&user="+user_name+
				((password!=null)?"&password="+password:"");
			System.out.println(db_url);
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(db_url);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void close() throws SQLException{
		conn.close();
	}
	
	public ResultSet executeQuery(String query){
		try{
			pstmt = conn.prepareStatement(query);
			return pstmt.executeQuery();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	public ResultSet executeQueryInStream(String query) throws SQLException{

		try{
			Statement stmt = conn.createStatement(
					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
			stmt.setFetchSize(Integer.MIN_VALUE);
			
			return stmt.executeQuery(query);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	public PreparedStatement getPreparedStatement(String query) throws SQLException{
		return conn.prepareStatement(query);
	}
	public int executeUpdate(PreparedStatement ps) throws SQLException{
		return ps.executeUpdate();
	}
	
}

